Namespace - LJCDBDataAccess
Returns
The database result XML message.
Syntax
C# |
private DbResult Add()
|
Inserts a record with the specified columns.
(E)
Remarks
This method is called from the
Execute()
method if the DbRequest.QueryType is "Insert".
Example
C# |
private static void Add(DbDataAccess dbDataAccess
, DbColumns dataDefinition)
{
List<string> columnNames = new List<string>();
foreach (DbColumn column in dataDefinition)
{
if (column.ColumnName != "Id")
{
columnNames.Add(column.ColumnName);
}
}
Person dataRecord = new Person()
{
Name = "TestName",
PrincipleFlag = false
};
var dataColumns = DbCommon.QueryDataColumns(dataRecord, columnList
, dataDefinition);
DbColumns keyColumns = GetKeyColumns(dataRecord, dataDefinition);
DbRequest dbRequest = new DbRequest()
{
Columns = dataColumns,
KeyColumns = keyColumns
QueryType = QueryType.Insert.ToString(),
TableName = "PersonTest",
};
DbResult dbResult = dbDataAccess.Execute(dbRequest);
if (dbResult != null)
{
string sqlStatement = dbResult.ExecutedSql;
if (dbResult.DbRecords != null && dbResult.DbRecords.Count > 0)
{
SetAssignedValues(dataRecord, dbResult.DbRecords[0]);
}
}
}
private static DbColumns GetKeyColumns(Person person
, DbColumns dataDefinition)
{
DbColumns retValue = null;
List<string> lookupColumnNames = new List<string>()
{
"Id",
"Name"
};
retValue = DbCommon.QueryDataKeys(person, dataDefinintion
, lookupColumnNames);
List<string> dbAssignedColumnNames = new List<string>()
{
"Id"
};
DbCommon.SetDatabaseAssignedKeys(retValue, dbAssignedColumnNames);
return retValue;
}
private static void SetAssignedValues(Person person, DbValues resultRecord)
{
Person addedPerson = new Person();
DbCommon.SetObjectValues(resultRecord, addedPerson);
person.ID = addedPerson.ID;
}
|